package fantasyday.wowzolo.web.account;

import fantasyday.wowzolo.domain.Account;
import fantasyday.wowzolo.service.AccountService;
import fantasyday.wowzolo.web.catalog.IndexPage;
import fantasyday.wowzolo.web.common.BasePage;
import fantasyday.wowzolo.web.common.ErrorMessage;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class RegisterPage extends BasePage {
	@SpringBean
	private AccountService accountService;

    @SuppressWarnings("serial")
	public RegisterPage() {
		Form form = new AccountForm("registerForm", new Account()) {
			@Override
			protected void onSubmit() {
                try {
                    Account account = (Account) getModelObject();
                    accountService.insertAccount(account);
                    account.setPassword(null);
                    getJpetstoreSession().setSignonedAccount(account);
                    setResponsePage(IndexPage.class);
                } catch (Exception e) {
                    error(ErrorMessage.registerError + e);
                }
            }
		};		
		
		form.add(new RequiredTextField("username"));
		
		add(form);
	}
}
